﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using Microsoft.Web.Preview.Services;
using BLL.WWW_WWW.Wiki;
using Model.WWW_WWW.Wiki;
using BLF.WWW_WWW.Wiki;
using Model;
using Lx.Web.Common.UI;
using Lx.Web.Common.ControlsUI;

namespace WebServices
{
    [WebService(Namespace = "http://www.italki.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class knowledge : System.Web.Services.WebService
    {
        [WebMethod]
        public ArrayList AddMessage(string pageName,string body,int l)
        {
            ArrayList outList = new ArrayList();
            CommonMessage msg = new CommonMessage();
            if (HttpContext.Current.User.Identity.Name == "")
            {
                msg.Msg = "l";
                outList.Add(msg);
                return outList;
            }
            if (string.IsNullOrEmpty(body) || body.Trim() == "")
            {
                msg.Msg = "n";
                outList.Add(msg);
                return outList;
            }
            msg = Wiki.AddMessage(pageName, body, l, out outList);
            if (!msg.State) msg.Msg = "s";
            outList.Insert(0, msg);
            return outList;
        }

        [WebMethod]
        public ArrayList AddMessage1(string pageName, string body, int l,int pageIndex)
        {
            ArrayList outList = new ArrayList();
            CommonMessage msg = new CommonMessage();
            if (HttpContext.Current.User.Identity.Name == "")
            {
                msg.Msg = "l";
                outList.Add(msg);
                return outList;
            }
            if (string.IsNullOrEmpty(body) || body.Trim() == "")
            {
                msg.Msg = "n";
                outList.Add(msg);
                return outList;
            }
            msg = Wiki.AddMessage(pageName, body, l,pageIndex, out outList);
            if (!msg.State) msg.Msg = "s";

            int k = (int)outList[1];
            outList.RemoveAt(1);
            outList.Insert(1,new BLF.PageableObject(k, 1, l));

            outList.Insert(0, msg);
            outList.Insert(3, k);
            return outList;
        }

        [WebMethod]
        public ArrayList DeleteComent(string pageName,int id,int l)
        {
            ArrayList outList = new ArrayList();
            CommonMessage msg = new CommonMessage();
            if (HttpContext.Current.User.Identity.Name == "")
            {
                msg.Msg = "l";
                outList.Add(msg);
                return outList;
            }
            if (!(new BLL.WWW_WWW.Wiki.WikiSystem()).IsAdmin(HttpContext.Current.User.Identity.Name))
            {
                msg.Msg = "A";
                outList.Add(msg);
                return outList;
            }
            if (!Pages.Instance.RemoveMessage(Pages.Instance.FindPage(pageName), id, true))
            {
                msg.Msg = "F";
                outList.Add(msg);
                return outList;
            }
            msg = Wiki.GetMessage(pageName, l, out outList);
            if (!msg.State) msg.Msg = "s";
            outList.Insert(0, msg);
            return outList;
        }

        [WebMethod]
        public ArrayList DeleteComent1(string pageName, int id, int l, int pageIndex)
        {
            ArrayList outList = new ArrayList();
            CommonMessage msg = new CommonMessage();
            if (HttpContext.Current.User.Identity.Name == "")
            {
                msg.Msg = "l";
                outList.Add(msg);
                return outList;
            }
            if (!(new BLL.WWW_WWW.Wiki.WikiSystem()).IsAdmin(HttpContext.Current.User.Identity.Name))
            {
                msg.Msg = "A";
                outList.Add(msg);
                return outList;
            }
            if (!Pages.Instance.RemoveMessage(Pages.Instance.FindPage(pageName), id, true))
            {
                msg.Msg = "F";
                outList.Add(msg);
                return outList;
            }
            msg = Wiki.GetMessage(pageName, l, out outList);
            if (!msg.State) msg.Msg = "s";

            int k = (int)outList[1];
            outList.RemoveAt(1);
            outList.Insert(1, new BLF.PageableObject(k, 1, l));

            outList.Insert(0, msg);
            return outList;
        }

        [WebMethod]
        public CommonMessage RequestEdit(string pageName)
        {
            CommonMessage msg = new CommonMessage();
            if (HttpContext.Current.User.Identity.Name == "")
            {
                msg.Msg = "l";
                return msg;
            }
            msg = Wiki.RequestEdit(pageName, HttpContext.Current.User.Identity.Name);
            return msg;
        }

        [WebMethod]
        public ArrayList Rate(string pageName,int v)
        {
            ArrayList outList = new ArrayList();
            CommonMessage msg = new CommonMessage();
            if (v<1||v>5)
            {
                outList.Add(msg);
                return outList;
            }
            msg = Wiki.Rate(pageName,Guid.NewGuid().ToString("N"),HttpContext.Current.Request.UserHostAddress,HttpContext.Current.User.Identity.Name, v,out outList);
            outList.Insert(0, msg);
            return outList;
        }
    }
}
